#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unordered_map>
using std::cout;
using std::endl;
static const int BUFFER_NUM = 1024;
class Udp_myshell
{
public:
    Udp_myshell(std::string ip = "", uint16_t port = 8080)
        : _port(port), _ip(ip)
    {
    }
    int Create()
    {
        _sock = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sock < 0)
        {
            cout << strerror(errno) << "    sockfail" << endl;
            exit(1);
        }
        return _sock;
    }
    void Bind()
    {
        //_ip = _ip.empty() ? INADDR_ANY : _ip;
        sockaddr_in serve;
        serve.sin_addr.s_addr = INADDR_ANY;
        // serve.sin_addr.s_addr = inet_addr(INADDR_ANY);
        serve.sin_family = AF_INET;
        serve.sin_port = htons(_port);
        int ret = bind(_sock, (sockaddr *)(&serve), sizeof(serve));
        if (ret < 0)
        {
            cout << strerror(errno) << endl;
            exit(2);
        }
    }
    ~Udp_myshell()
    {
        if (_sock > 0)
            close(_sock);
    }

private:
    int _sock;
    uint16_t _port;
    std::string _ip;
};
